package org.jeecg.modules.wlhy.hyopenapi.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.api.dto.openapi.RoutePathDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.third.openapi.OpenApiService;
import org.jeecg.modules.third.openapi.WaybillRouteService;
import org.jeecg.modules.wlhy.hyopenapi.service.IHyOpenAPIService;
import org.jeecg.modules.wlhy.shipping.entity.WaybillRouteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 监听司机SDK上报位置的业务处理
 */
@Service
@Slf4j
public class HyOpenAPIServiceImpl implements IHyOpenAPIService {

    @Autowired
    private OpenApiService openApiService;

    @Autowired
    private WaybillRouteService waybillRouteService;

    @Override
    public Result getRoutePath(RoutePathDTO routePathDTO) {

        if (StrUtil.isEmpty(routePathDTO.getVclN())) {
            return Result.error("车牌号不能为空");
        }

        if (StrUtil.isEmpty(routePathDTO.getVco())) {
            return Result.error("车牌颜色不能为空");
        }

        if (StrUtil.isEmpty(routePathDTO.getQryBtm())) {
            return Result.error("运单装货时间不能为空");
        }

        if (StrUtil.isEmpty(routePathDTO.getQryEtm())) {
            return Result.error("运单卸货时间不能为空");
        }

        String message = StrUtil.EMPTY;
        try {
            //中交轨迹和交通部字典值不一样，黄绿车牌交通部要求93，中交是3
            if ("93".equals(routePathDTO.getVco())) {
                routePathDTO.setVco("3");
            }

            WaybillRouteVO waybillRouteVO = waybillRouteService.getWaybillRoute(routePathDTO.getVclN(), routePathDTO.getVco(),
                    routePathDTO.getQryBtm(), routePathDTO.getQryEtm(), "Baidu");
            return Result.OK(waybillRouteVO);
        } catch(Exception e) {
            log.error("获取中交轨迹失败", e.getMessage());
            message = e.getMessage();
        }

        return Result.OK(message, null);
    }

    /**
     *{
     * 	"status": 1001,
     * 	"result": {
     * 		"firstVcl": {
     * 			"lat": "21896964",
     * 			"lon": "68032728",
     * 			"adr": "山西省长治市黎城县仁庄村教育东街1号西北方向110米黎城桥东轮胎大世界,西方向,29.0米",
     * 			"utc": "1695018354000",
     * 			"spd": "19.0",
     * 			"drc": "30",
     * 			"province": "山西省",
     * 			"city": "长治市",
     * 			"country": "黎城县",
     * 			"mil": "75182.4",
     * 			"vno": "冀E6745Y",
     * 			"status": "1001",
     * 			"offlineState": false,
     * 			"offlineTime": "",
     * 			"runDistance": "",
     * 			"remainDistance": "",
     * 			"estimateArriveTime": ""
     *                },
     * 		"others": []* 	}
     * }
     * @return
     */
    @Override
    public Result getTransTimeManage(List<RoutePathDTO> routePathDTOList) {

        List<String> vcNoList = new ArrayList<>();
        for (RoutePathDTO routePathDTO : routePathDTOList) {
            if (StrUtil.isNotEmpty(routePathDTO.getVclN()) && StrUtil.isNotEmpty(routePathDTO.getVco())) {
                String vcNos = routePathDTO.getVclN() + "_" + routePathDTO.getVco();
                vcNoList.add(vcNos);
            }
        }

        if (CollUtil.isEmpty(vcNoList)) {
            return Result.error("参数错误");
        }

        String vcNos = vcNoList.stream().collect(Collectors.joining(","));
        String result = this.openApiService.transTimeManageV3(vcNos);
        return Result.OK(result);
    }

}
